package ru.reinvent.perspectiva.net;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import ru.reinvent.perspectiva.cash.Lot;
import ru.reinvent.perspectiva.cash.Vendor;

public class PerspectivaXMLParser 
	{
	
	public static final String TAG_VENDOR = "vendor";
	public static final String TAG_TITLE = "title";
	
	public static final String TAG_LOT = "lot";
	public static final String TAG_NAME = "name";
	public static final String TAG_LINK = "link";
	public static final String TAG_ADDDATE = "adddate";
	public static final String TAG_PRICE = "price";
	public static final String TAG_REFERENCE = "refference";
	public static final String TAG_IMAGE = "image";
	public static final String TAG_CASE = "case";
	public static final String TAG_GENDER = "gender";
	public static final String TAG_TYPE = "type";
	public static final String TAG_DESC = "description";
	public static final String TAG_STATE = "state";
	public static final String TAG_REGION = "region";
	
	public ArrayList<Vendor> parse(String xmlStr) throws XmlPullParserException, IOException
		{
		ArrayList<Vendor> ret = new ArrayList<Vendor>();
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(new StringReader(xmlStr));
        
        int event;
        while ((event=xpp.next()) != XmlPullParser.END_DOCUMENT)
        	if (event==XmlPullParser.START_TAG && TAG_VENDOR.equals(xpp.getName()))
        		ret.add(createVendor(xpp));
		
		return ret;
		}

	public Vendor createVendor(XmlPullParser xpp) throws XmlPullParserException, IOException 
		{
		skipToTag(xpp,TAG_TITLE);
		skipToText(xpp);
		
    	Vendor vendor = new Vendor();
    	vendor.setName(xpp.getText());

        int event;
        while (!((event=xpp.next())==XmlPullParser.END_TAG && TAG_VENDOR.equals(xpp.getName())))
        	if (event==XmlPullParser.START_TAG && TAG_LOT.equals(xpp.getName()))
        		vendor.addLot(createLot(xpp));
    	
		return vendor;
		}

	public Lot createLot(XmlPullParser xpp) throws XmlPullParserException, IOException 
		{
		Lot lot = new Lot();
		
        int event;
        while (!((event=xpp.next())==XmlPullParser.END_TAG && TAG_LOT.equals(xpp.getName())))
        	{
        	if (event != XmlPullParser.START_TAG) continue;

        	if (TAG_NAME.equals(xpp.getName())) { skipToText(xpp); lot.setName(xpp.getText()); }
        	else if (TAG_LINK.equals(xpp.getName())) { skipToText(xpp); lot.setLink(xpp.getText()); }
        	else if (TAG_ADDDATE.equals(xpp.getName())) { skipToText(xpp); lot.setAddDate(xpp.getText()); }
        	else if (TAG_PRICE.equals(xpp.getName())) { skipToText(xpp); lot.setPrice(xpp.getText()); }
        	else if (TAG_REFERENCE.equals(xpp.getName())) { skipToText(xpp); lot.setReference(xpp.getText()); }
        	else if (TAG_IMAGE.equals(xpp.getName())) { skipToText(xpp); lot.setImageUrl(xpp.getText()); }
        	else if (TAG_CASE.equals(xpp.getName())) { skipToText(xpp); lot.setHull(xpp.getText()); }
        	else if (TAG_GENDER.equals(xpp.getName())) { skipToText(xpp); lot.setGender(xpp.getText()); }
        	else if (TAG_TYPE.equals(xpp.getName())) { skipToText(xpp); lot.setType(xpp.getText()); }
        	else if (TAG_DESC.equals(xpp.getName())) { skipToText(xpp); lot.setDescription(xpp.getText()); }
        	else if (TAG_STATE.equals(xpp.getName())) { skipToText(xpp); lot.setState(xpp.getText()); }
        	else if (TAG_REGION.equals(xpp.getName())) { skipToText(xpp); lot.setRegion(xpp.getText()); }
        	}
        
		return lot;
		}
	
	protected void skipToTag(XmlPullParser xpp, String tag) throws XmlPullParserException, IOException
		{
        while (xpp.next()!=XmlPullParser.START_TAG && tag.equals(xpp.getName())) {;}
		}
	
	protected void skipToText(XmlPullParser xpp) throws XmlPullParserException, IOException
		{
        while (xpp.next()!=XmlPullParser.TEXT) {;}
		}
	}
